package txt;

import nl.siegmann.epublib.domain.*;
import nl.siegmann.epublib.epub.EpubWriter;
import nl.siegmann.epublib.service.MediatypeService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;

public class EpubWriter1 extends Translator {

    public static void main(String[] args) {
        String inputDirectoryPath = "C:\\Project\\data-extraction\\temp\\krishnamurti\\";
        String outputEpubFilePath = "C:\\Project\\data-extraction\\temp\\krishnamurti\\克里希那穆提全集.epub";

        try {
            // 创建EPUB书籍对象
            Book book = new Book();
            Metadata metadata = book.getMetadata();
            metadata.addTitle("克里希那穆提全集");
            metadata.addAuthor(new Author("krishnamurti"));
            // 获取目录中的所有文件夹
            File inputDirectory = new File(inputDirectoryPath);
            if (inputDirectory.isDirectory()) {
                File[] subdirectories = inputDirectory.listFiles(File::isDirectory);

                if (subdirectories != null) {
                    // 遍历每个文件夹
                    for (File subdirectory : subdirectories) {
                        String folderName = subdirectory.getName();
                        File chapter = Arrays.stream(Objects.requireNonNull(subdirectory.listFiles((dir, name) -> "chapter.html".equals(name)))).findFirst().get();
                        Document parse = Jsoup.parse(chapter, "UTF-8");
                        Resource resource = new Resource(parse.html().getBytes(), chapter.getName());
//                        // 创建一个1级目录的
                        TOCReference chapter1 = book.addSection(folderName, resource);

                        // 获取文件夹中的所有HTML文件
                        File[] htmlFiles = subdirectory.listFiles((dir, name) -> name.toLowerCase().endsWith(".html") && !"chapter.html".equals(name));

                        if (htmlFiles != null && htmlFiles.length > 0) {
                            // 将每个HTML文件添加到Section中
                            for (File htmlFile : htmlFiles) {
                                Document document = Jsoup.parse(htmlFile, "UTF-8");
                                String title = document.title();
                                book.addSection(chapter1, title, new Resource(document.html().getBytes(), htmlFile.getName()));
                            }
                        } else {
                            File c = Objects.requireNonNull(subdirectory.listFiles())[0];
                            if (c.isFile()) {
                                Document document = Jsoup.parse(c, "UTF-8");
                                String title = document.title();
                                Resource resource1 = new Resource(document.html().getBytes(), title);
                                resource1.setMediaType(MediatypeService.XHTML);
                                book.addSection(chapter1, title, resource1);
                            }
                        }
                    }
                }
            }

            // 将EPUB书籍写入文件
            EpubWriter epubWriter = new EpubWriter();
            epubWriter.write(book, new FileOutputStream(outputEpubFilePath));
            System.out.println("EPUB文件已创建：" + outputEpubFilePath);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
